import random

import pandas as pd

class AddUserData(object):

    """
    插入数据
    """
    def __init__(self, filename="24153_analysis_watchstatistics.csv", rows=551):

        self.read_data = pd.read_csv(filename)
        self.data = self.read_data[:514]

    def insert_into_data(self):
        user_id_list = []

        while True:
            if len(user_id_list) >= 514:
                break
            id = random.randrange(170000, 171000, 1)
            if id in user_id_list:
                continue
            else:
                user_id_list.append(id)

        for i in range(0, 514):
            user_id = user_id_list[i]
            self.data['user_id'][i] = user_id

        self.data.to_csv("insert_into.csv", index=False)


class StatisticsUser(object):

    def __init__(self, filename='insert_into.csv'):
        self.read_data = pd.read_csv(filename)

    def statistic_user_data(self, bit_rate=1426, minutes=139):
        _data = []
        for i in range(len(self.read_data)):
            user_id = self.read_data['user_id'][i]
            duration = self.read_data['duration'][i]
            flow = float(duration) * bit_rate * minutes
            flow = flow / 8 / 1024
            end_time = self.read_data['endDate'][i]
            _data.append([user_id, flow, duration, end_time])

        _new_data = pd.DataFrame(data=_data, columns=['用户id', '流量', '观看时长', '结束时间'])
        _new_data.to_csv("new_data.csv", index=False)


if __name__ == '__main__':
    user_data = AddUserData()
    user_data.insert_into_data()
    su = StatisticsUser()
    su.statistic_user_data()



